package com.huluobo.a2208mdmdemo2.myview

import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.RelativeLayout
import com.huluobo.a2208mdmdemo2.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.util.Random
import java.util.Timer
import kotlin.concurrent.timerTask

/**
 *  Created by LC on 2025/2/13.
 */
class RedPackView(context: Context?) : RelativeLayout(context) {
    lateinit var timer: Timer

    fun start() {
        timer = Timer()
        timer.schedule(timerTask {
            CoroutineScope(Job()).launch(Dispatchers.Main) {
                val iv = ImageView(context)
                iv.setImageResource(R.drawable.hb)
                val num = Random().nextInt(1000)

                val params = LayoutParams(100, 200)
                params.topMargin = 0
                params.leftMargin = num
                iv.layoutParams = params

                val animator = ObjectAnimator.ofFloat(iv, "translationY", 0f, 2000f)
                animator.duration = 5000
                animator.start()
                addView(iv)
            }
        }, 0, 200)
    }

    fun stop() {
        timer.cancel()
    }
}